<template>
  <div class="address-manage">
    <search-table :url="url" ref="searchtable" :form="form" :columns="columns">

      <template slot="searchForm">
        <a-form layout="inline" :form="form">
          <a-form-item label="卖点名称">
            <a-input
              :max-length="20"
              v-decorator="[
                'pointName',
              ]"
              placeholder="请输入卖点名称"
            />
          </a-form-item>
          <a-form-item label="卖点类型">
            <a-select
              style="width:150px"
              allow-clear
              :options="typeOptions"
              :max-length="20"
              v-decorator="[
                'pointType',
              ]"
              placeholder="请选择卖点类型"
            />
          </a-form-item>
          <a-form-item label="创建时间">
            <fast-range-picker style="width:240px"
              v-decorator="[
                'startTime-endTime'
              ]"
              />
          </a-form-item>
        </a-form>
      </template>
      <template slot="oprationRow">
        <permission authority="sellpoint:add">
          <a-button type="primary" icon="plus" @click="openInfo()">添加</a-button>
        </permission>
        <permission authority="sellpoint:delete">
          <a-button @click="deleteRows($refs.searchtable.selectedRowKeys)" type="danger" icon="delete">删除</a-button>
        </permission>
      </template>
      <template slot="tableOperation" slot-scope="{record}">
        <permission authority="sellpoint:modify">
          <span class="text-primary pointer" @click="openInfo(record)">修改</span>
        </permission>
        <permission authority="sellpoint:delete">
          <a-divider type="vertical" />
          <span class="text-danger pointer" @click="deleteRows([record.id])">删除</span>
        </permission>
      </template>
    </search-table>
    <product-selling-info  :record="currentRecord" :typeOptions="typeOptions" @cancel="closeInfoModal" :visible="infoVisible" />
  </div>
</template>
<script>
import ProductSellingInfo from './components/ProductSellingInfo'
import {findNameInOptions} from '@/utils'
import {createOptionsByDict} from '@/utils/dict'
export default {
  name: 'ProductSelling',
  data () {
    return {
      url: '/sellpoint/page',
      infoVisible: false,
      currentRecord: {},
      form: this.$form.createForm(this, { name: 'table_search' }),
      typeOptions: createOptionsByDict('SELLING_POINT_TYPE'),
      columns:  [
        {
          title: '卖点名称',
          dataIndex: 'pointName',
        },
        {
          title: '卖点类型',
          dataIndex: 'pointType',
          customRender: text => findNameInOptions(this.typeOptions, text),
        },
        {
          title: '创建时间',
          dataIndex: 'createDate',
        },
        {
          title: '添加员工',
          dataIndex: 'createByName',
        },
        // {
        //   title: '排序',
        //   dataIndex: 'sort',
        // },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
        },]
    }
  },
  components: {
    ProductSellingInfo,
  },
  methods:{
    closeInfoModal (freshTable) {
      this.infoVisible = false
      if (freshTable) {
        this.$refs.searchtable.refreshTable()
      }
    },
    openInfo (record) {
      this.currentRecord = record
      this.infoVisible = true
    },
    deleteRows (ids) {
      if (!(ids && ids.length)) return this.$message.warn('请先选择一项后再进行操作')
      this.$confirm({
        title: '删除提示',
        content: '删除操作不可恢复，请确认是否删除？',
        okText: '确认删除',
        cancelText: '取消',
        onOk: ()=> {
          this.$post('/sellpoint/delete', ids).then(res => {
            if (res && res.returnCode == 1000) {
              this.$message.success('删除成功！')
              this.$refs.searchtable.refreshTable()
            }
          })
        }
      })
    },
  }
}
</script>
<style lang="less" scoped>

</style>